//
//  MBDMyIntegrationCell.swift
//  MasterBoy
//
//  Created by kyson on 2024/10/12.
//

import Foundation
import SnapKit
import YYText



class MBDMyIntegrationCell: MBDCommonCell {

    var consumptionLabel : YYLabel!
    var timeLabel : YYLabel!
    var priceDetailLabel : YYLabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.initialize()
    }
    
    override var cellModel: Any! {
        didSet {
            let cellModel = self.cellModel as! MBDMyIntegrationCellModel
            
            self.integrateDetail = cellModel.integrationDetail
            self.location = cellModel.location
        }
    }

    func initialize() {
        self.contentView.setDynamicForgroundColor()
        self.commonBackgroundView.setDynamicForgroundColor()
        
        consumptionLabel = YYLabel()
        self.commonBackgroundView.addSubview(consumptionLabel)
        
        timeLabel = YYLabel()
        self.commonBackgroundView.addSubview(timeLabel)
        
        priceDetailLabel = YYLabel()
        priceDetailLabel.textColor = .red
        priceDetailLabel.textAlignment = .right
        priceDetailLabel.font = UIFont.systemFont(ofSize: 28)
        self.commonBackgroundView.addSubview(priceDetailLabel)
        
        consumptionLabel.snp.makeConstraints { make in
            make.left.equalTo(MBDMyIntegrationCellModel.consumptionLeftMargin)
            make.top.equalTo(MBDMyIntegrationCellModel.topMargin)
            make.right.lessThanOrEqualTo(self.priceDetailLabel.snp.left).offset(-MBDMyIntegrationCellModel.priceDetailLabelLeftMargin)
            make.height.equalTo(0)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(consumptionLabel)
            make.top.equalTo(consumptionLabel.snp.bottom).offset(MBDMyIntegrationCellModel.marginBetweenConsumptionAndTime)
            make.right.lessThanOrEqualTo(self.priceDetailLabel.snp.left).offset(-MBDMyIntegrationCellModel.priceDetailLabelLeftMargin)
            make.height.equalTo(16)
        }
        
        priceDetailLabel.snp.makeConstraints { make in
            make.right.equalTo(self.commonBackgroundView).offset(-MBDMyIntegrationCellModel.priceDetailLabelRightMargin)
            make.width.equalTo(MBDMyIntegrationCellModel.priceDetailLabelWidth)
            make.centerY.equalTo(self.commonBackgroundView)
        }
    }
    
    var integrateDetail : MBDIntegrateExpDetailModel? {
        didSet {
            let cellModel = self.cellModel as! MBDMyIntegrationCellModel
            
            consumptionLabel.textLayout = cellModel.consumptionLayout
            consumptionLabel.snp.updateConstraints { make in
                let height = cellModel.consumptionLayout!.textBoundingRect.height
                make.height.equalTo(height)
            }
            
            timeLabel.textLayout = cellModel.timeLayout

            timeLabel.snp.updateConstraints { make in
                let height = cellModel.timeLayout!.textBoundingSize.height
                make.height.equalTo(height)
            }
            
            priceDetailLabel.text = integrateDetail?.integralNumber
        }
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
    }

    
   
}
